/*******************************************************************************
* CSCI321 Major Project 2010 - University of Wollongong                        *
********************************************************************************
* Program:		WeaveEdit                                              *
* Authors:		Simon Allen (Student#: 3457527, Unix: sa655)           *
*			Scott Bellis (Student#: 3479110, Unix: sjb900)         *
*			Jason Benger (Student#: 3454137, Unix: jb960)          *
*			Timothy Newmarch (Student#: 3459263 Unix: tgn336)      *
*			Benjamin Richardson (Student# 3467752, Unix: bjr401)   *
* Supervisor:		Daniel F. Saffioti                                     *
* Modified:		28/10/2010                                             *
* Description:		WeaveEdit is a highly portable collaborative text      *
*			editor and word processor, allowing several users to   *
*			read and edit multiple documents at the same time.     *
*			It is available for Mac OS X, Windows and Linux,       *
*			utilising Nokia's Qt development framework.            *
*******************************************************************************/


#include "statistics.h"
#include "ui_statistics.h"
#include "MainWindow/mainwindow.h"


Statistics::Statistics(QWidget *parent) : QDialog(parent), ui(new Ui::Statistics)
{
	editor = NULL;

	ui->setupUi(this);
}

Statistics::~Statistics()
{
	delete ui;
}

/**
  * Link to an editor, so that the dialog updates in real time.
  */
void Statistics::show(Editor *newEditor)
{
	QDialog::show();

	editor = newEditor;

	updateStats();

	// MIGHT have to disconnect the signal before this if the editor changes, but I'm not sure if it's needed.
	connect(editor, SIGNAL(textChanged()), this, SLOT(updateStats()));
	connect(editor, SIGNAL(selectionChanged()), this, SLOT(updateStats()));
}

/**
  * Updates stats for selected text, document and all document sections.
  */
void Statistics::updateStats()
{
	if (editor)
	{
		QTextDocument *doc = editor->document();
		QTextCursor cursor = editor->textCursor();
		QTextDocument cursorDoc(cursor.selectedText());
		QList<Editor*>allEditors = MainWindow::getInstance()->getEditors();
		int allEditorChars = 0, allEditorCharsExSpaces = 0;
		int allEditorWords = 0, allEditorLines = 0;

		// Set statistics for selected text.
		ui->label_selectedChars->setNum(cursor.selectedText().length());
		ui->label_selectedCharsExcludingSpaces->setNum(cursor.selectedText().remove(QRegExp("\\s+")).length());

		if (cursor.selectedText().length() > 0)
			ui->label_selectedWords->setNum(cursor.selectedText().simplified().split(QRegExp("\\s+")).count());

		else
			ui->label_selectedWords->setNum(0);

		// Line count is always one if there is no selection (which isn't right), so check for that.
		if (cursor.hasSelection())
			ui->label_selectedLines->setNum(cursorDoc.lineCount());

		else
			ui->label_selectedLines->setNum(0);

		// Set statistics for current editor.
		ui->label_docChars->setNum(doc->toPlainText().length());
		ui->label_docCharsExcludingSpaces->setNum(doc->toPlainText().remove(QRegExp("\\s+")).length());

		if (doc->toPlainText().length() > 0)
			ui->label_docWords->setNum(doc->toPlainText().simplified().split(QRegExp("\\s+")).count());

		else
			ui->label_docWords->setNum(0);

		ui->label_docLines->setNum(doc->lineCount());

		// Set statistics for all editors.
		foreach (Editor *ed, allEditors)
		{
			allEditorChars += ed->document()->toPlainText().length();
			allEditorCharsExSpaces += ed->document()->toPlainText().remove(QRegExp("\\s+")).length();

			if (ed->document()->toPlainText().length() > 0)
				allEditorWords += ed->document()->toPlainText().simplified().split(QRegExp("\\s+")).count();

			allEditorLines += ed->document()->lineCount();
		}

		ui->label_allDocChars->setNum(allEditorChars);
		ui->label_allDocCharsExcludingSpaces->setNum(allEditorCharsExSpaces);
		ui->label_allDocWords->setNum(allEditorWords);
		ui->label_allDocLines->setNum(allEditorLines);
	}
}
